import { _decorator, Component, EventTouch, Label, Node, tween, v3 } from 'cc'
const { ccclass, property } = _decorator

@ccclass('GameRoot')
export class GameRoot extends Component {
  @property(Node) buttonLeft: Node
  @property(Node) buttonRight: Node
  @property(Node) image: Node
  //   @property(Node) label1: Node
  //   @property(Node) label2: Node

  start() {
    this.buttonLeft.on(
      Node.EventType.TOUCH_START,
      (event: EventTouch) => {
        console.log(6666)
        this.buttonLeft.setScale(0.9, 0.9)
      },
      this
    )

    this.buttonLeft.on(
      Node.EventType.TOUCH_END,
      (event: EventTouch) => {
        this.buttonLeft.setScale(1, 1)
        const obj = {
          n: this.image.position.x,
        }
        tween(obj)
          .to(
            1,
            { n: this.image.position.x - 50 },
            {
              onUpdate: (target, ratio) => {
                this.image.setPosition(obj.n, this.image.position.y)
              },
            }
          )
          .start()
      },
      this
    )

    this.buttonRight.on(
      Node.EventType.TOUCH_START,
      (event: EventTouch) => {
        console.log(6666)
        this.buttonRight.setScale(0.9, 0.9)
      },
      this
    )

    this.buttonRight.on(
      Node.EventType.TOUCH_END,
      (event: EventTouch) => {
        this.buttonRight.setScale(1, 1)
        const obj = {
          n: this.image.position.x,
        }
        tween(obj)
          .to(
            1,
            { n: this.image.position.x + 50 },
            {
              onUpdate: (target, ratio) => {
                this.image.setPosition(obj.n, this.image.position.y)
              },
            }
          )
          .start()
      },
      this
    )

    this.myAnimation()
  }

  myAnimation() {
    // tween(this.image)
    //   .to(1, { position: v3(0, 200, 0) })
    //   .start()

    const obj = {
      n: 0,
    }

    // const label1 = this.label1.getComponent(Label)

    // 跳跃效果
    tween(obj)
      .repeatForever(
        tween(obj)
          .to(
            0.5,
            { n: 200 },
            {
              onUpdate: (target, ratio) => {
                // label1.string = obj.n.toFixed(2) + ''
                this.image.setPosition(this.image.position.x, obj.n)
              },
              easing: 'quadOut',
            }
          )
          .to(
            0.5,
            { n: 0 },
            {
              onUpdate: (target, ratio) => {
                // label1.string = obj.n.toFixed(2) + ''
                this.image.setPosition(this.image.position.x, obj.n)
              },
              easing: 'quadIn',
            }
          )
      )
      .start()
  }

  update(deltaTime: number) {}
}
